<template>
  <div class="warp_auto">
    <div class="warp_top">
      <div class="jiansuo-box">
        字典名称：
        <el-input
          v-model="commonName"
          clearable
          class="input"
          placeholder="请输入名称"
        />
      </div>
      <div class="jiansuo-box">
        状态：
        <el-select
          v-model="switChState"
          placeholder="请选择"
          class="input"
          clearable
        >
          <el-option
            v-for="item in options"
            :key="item.id"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </div>

      <el-button
        type="primary"
        icon="el-icon-search"
        class="btn"
        @click="btn"
      >检索</el-button>
      <el-button
        type="primary"
        icon="el-icon-plus"
        class="btn"
        @click="add"
      >新增字典项</el-button>
    </div>

    <!-- 子项字典表格明细 -->
    <el-card>
      <el-table
        :data="arr"
        stripe
        :header-cell-style="{ background: '#d2e0f9', color: '#606266' }"
        :cell-style="{'text-align':'center'}"
      >
        <el-table-column type="index" label="序号" align="center" />
        <el-table-column label="字典编码" min-width="40%" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.common_item_code }}</span>
          </template>
        </el-table-column>
        <el-table-column label="字典名称" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.common_item_name }}</span>
          </template>
        </el-table-column>
        <el-table-column label="描述" align="center">
          <template slot-scope="scope">
            <span style="margin-left: 10px">{{ scope.row.description }}</span>
          </template>
        </el-table-column>
        <el-table-column label="排序"  align="center">
          <template slot-scope="scope">
            <span style="margin-left: 10px">{{ scope.row.sort }}</span>
          </template>
        </el-table-column>
        <el-table-column label="字典项名称"  align="center">
          <template slot-scope="scope">
            <span style="margin-left: 10px">{{ scope.row.common_name }}</span>
          </template>
        </el-table-column>

        <el-table-column label="类型"  align="center">
          <template slot-scope="scope">
            <span style="margin-left: 10px">{{ dictType }}</span>
          </template>
        </el-table-column>

        <el-table-column
          property="status"
          label="状态"
          align="center"
          header-align="center"
        >
          <template scope="scope">
            <el-switch
              v-model="scope.row.status"
              class="switch-btn"
              inactive-text="禁用"
              active-text="启用"
              :active-value="1"
              :inactive-value="0"
              active-color="#02538C"
              inactive-color="#B9B9B9"
              @change="changeSwitchs(scope.$index, scope.row)"
            />
          </template>
        </el-table-column>


        <el-table-column label="操作" width="200" align="center">
          <template slot-scope="scope">
            <el-button size="mini" @click="handleEdit(scope.row)">编辑</el-button>
            <el-popover
              :ref="`popover-${scope.$index}`"
              trigger="click"
              placement="top"
              width="100"
            >
              <p class="el-icon-warning">确定删除</p>
              <div style="text-align: right; margin: 0">
                <el-button size="mini" type="text" @click="no(scope)"
                >取消</el-button
                >
                <el-button type="primary" size="mini" @click="yes(scope)"
                >确定</el-button
                >
              </div>
              <el-button
                slot="reference"
                size="mini"
                style="margin-left: 10px"
                type="danger"
              >删除</el-button
              >
            </el-popover>
          </template>
        </el-table-column>
      </el-table>

      <div class="paging">
        <el-pagination
          :current-page="start"
          :page-size="PageSize"
          :pager-count="5"
          layout="total, sizes, prev, pager, next, jumper"
          :total="totalCount"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>

    <!-- 新增子项字典 -->
    <el-dialog
      title="新增字典项"
      :visible.sync="addDialogVisible"
      class="mod_box"
      :close-on-click-modal="false"
    >
      <el-form
        ref="ruleForm"
        :model="ruleForm"
        :rules="rules"
        class="demo-ruleForm"
        label-width="100px"
      >
        <el-form-item label="字典编码：" prop="commonItemCode">
          <el-input
            v-model="ruleForm.commonItemCode"
            class="el-inputer"
            placeholder="请输入字典编码"
          />
        </el-form-item>
        <el-form-item label="字典名称：" prop="commonItemName">
          <el-input
            v-model="ruleForm.commonItemName"
            class="el-inputer"
            placeholder="请输入字典名称"
          />
        </el-form-item>
        <el-form-item label="类型名称：" prop="type">
          <el-input
            v-model="dictTypeName"
            class="el-inputer"
            disabled
          />
        </el-form-item>
        <el-form-item label="描述：">
          <el-input
            v-model="ruleForm.description"
            type="textarea"
            :rows="3"
            class="el-inputer"
            placeholder="请输入描述"
          />
        </el-form-item>
        <el-form-item label="排序：">
          <el-input
            v-model="ruleForm.sort"
            class="el-inputer"
            placeholder="请输入排序"
          />
        </el-form-item>
        <el-form-item label="状态：" prop="status">
          <el-switch
            v-model="ruleForm.status"
            class="switch-btn"
            inactive-text="禁用"
            active-text="启用"
            :active-value="1"
            :inactive-value="0"
            active-color="#02538C"
            inactive-color="#B9B9B9"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="addDialogVisible = false">取 消</el-button>
        <el-button
          type="primary"
          @click="submitForm_leix('ruleForm')"
        >保 存</el-button
        >
      </div>
    </el-dialog>

    <!-- 编辑子项字典 -->
    <el-dialog
      title="编辑字典信息"
      :visible.sync="editDialogVisible"
      :close-on-click-modal="false"
      class="mod_box"
      :showClose="false"
    >
      <el-form class="form" label-width="90px">
        <el-form-item label="字典编码：" label-width="91px">
          <el-input v-model="dialogInfo.common_item_code" />
        </el-form-item>
        <el-form-item label="字典名称：" label-width="91px">
          <el-input v-model="dialogInfo.common_item_name" />
        </el-form-item>
        <el-form-item label="描述：">
          <el-input v-model="dialogInfo.description" type="textarea" :rows="3" />
        </el-form-item>
        <el-form-item label="类型名称：">
          <el-input :value="dictTypeName" disabled/>
        </el-form-item>
        <el-form-item label="排序：" label-width="91px">
          <el-input v-model="dialogInfo.sort" class="bianji" />
        </el-form-item>
        <el-form-item label="状态：">
          <el-switch
            v-model="dialogInfo.status"
            class="switch-btn"
            inactive-text="禁用"
            active-text="启用"
            :active-value="1"
            :inactive-value="0"
            active-color="#02538C"
            inactive-color="#B9B9B9"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible= false">取 消</el-button>
        <el-button type="primary" @click="editSaveClick">保 存</el-button>
      </div>
    </el-dialog>

    <!-- switch -->
    <el-dialog title="状态更新" :visible.sync="dialogUpdata">
      <span>
        <i class="el-icon-warning" />此操作将会改变开关状态, 是否继续?</span
      >
      <span slot="footer" class="dialog-footer">
        <el-button @click="(dialogUpdata = false), cancelSwitch()"
          >取 消</el-button
        >
        <el-button
          type="primary"
          @click="(dialogUpdata = false), updataSwitch()"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { Message } from "element-ui";
import {
  commentItemgetlist,
  commentItemdelete,
  commentIteminsert,
  commentItemupdate, commentItemedit,
} from "@/api/table";

export default {
  data() {
    return {
      dictType: '', //通用字典类型
      dictTypeName: '', //通用字典类型名称
      dialogUpdata: false, // 开关
      switchId: "",
      switchStatus: "",
      // 控制弹窗 显示
      editDialogVisible: false,
      // 点击查看按钮  这条数据详细信息
      dialogInfo: {},
      commonItemName: "",
      addDialogVisible: false,
      description: "",
      ruleForm: {
        commonItemCode: "",
        commonItemName: "",
        description: "",
        status: 1,
        sort:"",
      },
      rules: {
        commonItemCode: [
          { required: true, message: "请输入字典编码", trigger: "blur" },
        ],
        commonItemName: [
          { required: true, message: "请输入字典名称", trigger: "blur" },
        ]
      },
      options: [
        {
          id: "0",
          value: 1,
          label: "启用",
        },
        {
          id: "1",
          value: 0,
          label: "禁用",
        },
      ],
      switChState: "",
      value2: "",
      arr: [],
      arrleix: [], // 新增类型
      start: 1, // 默认显示第几页
      totalCount: 0, // 总条数，根据接口获取数据长度(注意：这里不能为空)
      pgeSizes: [10, 20, 30, 40], // 个数选择器（可修改）
      PageSize: 10, // 默认每页显示的条数（可修改）
      commonName: "",
      status: "",
      roleName: "",
    };
  },
  created() {
    this.dictType = this.$route.query.dictType;
    this.dictTypeName = this.$route.query.dictTypeName;
    this.commentItemgetlist(this.PageSize, this.start);
  },
  methods: {
    yes(scope) {
      // 可以在这里执行删除数据的回调操作.......删除操作......
      const id = scope.row.id;
      commentItemdelete({ id: id }).then((res) => {
        this.$message({
          message: "删除成功",
          type: "success",
        });
        this.commentItemgetlist(this.PageSize, this.start);
      });

      scope._self.$refs[`popover-${scope.$index}`].doClose();
    },

    no(scope) {
      this.$message("取消删除");
      scope._self.$refs[`popover-${scope.$index}`].doClose();
    },
    add(){
      this.ruleForm={
        commonItemCode: "",
          commonItemName: "",
          description: "",
          status: 1,
          sort:"",
      };
      this.addDialogVisible = true
    },
    // 新增类型
    submitForm_leix(ruleForm) {
      this.$refs[ruleForm].validate((valid) => {
        if (valid) {
          commentIteminsert({
            commonItemCode: this.ruleForm.commonItemCode,
            commonItemName: this.ruleForm.commonItemName,
            type: this.dictType,
            description: this.ruleForm.description,
            status: this.ruleForm.status,
            sort:this.ruleForm.sort === '' ? null : Number(this.ruleForm.sort),
          }).then((res) => {
            if (res.code === 3) {
              Message({
                message: res.msg,
                type: "error",
                duration: 5 * 1000,
              });
              this.addDialogVisible = true;
            } else {
              this.arrleix = res.data;
              this.addDialogVisible = false;
            }
            this.commentItemgetlist(this.PageSize, this.start);
          });
        } else {
          this.$message({
            message: "请填写必填项！",
            type: "warning",
            duration: 1500,
          });
        }
      });
    },

    // 列表开关的id
    changeSwitchs(index, row) {
      this.switchId = row.id;
      this.switchStatus = row.status;
      this.dialogUpdata = true;
    },
    // 保存开关状态-确认
    updataSwitch() {
      commentItemupdate({
        id: this.switchId,
        status: this.switchStatus,
      }).then((res) => {
        this.commentItemgetlist(this.PageSize, this.start);
      });
    },
    // 保存开关状态-取消
    cancelSwitch() {
      this.commentItemgetlist(this.PageSize, this.start); // 重新获取表格
    },
    // 搜索按钮
    btn() {
      this.start = 1;
      commentItemgetlist({
        commonItemName: this.commonName,
        type: this.dictType,
        size: this.PageSize,
        start: this.start,
        status: this.switChState === '' ? null : this.switChState,
      }).then((res) => {
        this.arr = res.data;
        this.totalCount = res.totalCount;
      });
    },
    handleEdit(info) {
      // 弹窗打开
      this.editDialogVisible = true;
      this.dialogInfo = info;
    },

    commentItemgetlist(pageSize, start) {
      commentItemgetlist({
        size: pageSize,
        start: start,
        commonItemName: this.commonName,
        type: this.dictType,
        status: this.switChState === '' ? null : this.switChState,
      }).then((res) => {
        this.arr = res.data;
        this.totalCount = res.totalCount;
      });
    },
    // 每页显示的条数
    handleSizeChange(val) {
      // 改变每页显示的条数
      this.PageSize = val;
      // 点击每页显示的条数时，显示第一页
      this.commentItemgetlist(val, 1);
      // 注意：在改变每页显示的条数时，要将页码显示到第一页
      this.start = 1;
    },
    // 显示第几页
    handleCurrentChange(val) {
      // 改变默认的页数
      this.start = val;

      // 切换页码时，要获取每页显示的条数
      this.commentItemgetlist(this.PageSize, this.start);
    },
    change: function (index, row) {
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    // 编辑保存
    editSaveClick() {
      commentItemedit({
        id: this.dialogInfo.id,
        commonItemCode: this.dialogInfo.common_item_code,
        status: this.dialogInfo.status,
        commonItemName: this.dialogInfo.common_item_name,
        type: this.dictType,
        description: this.dialogInfo.description,
        sort:this.dialogInfo.sort === '' ? null : Number(this.dialogInfo.sort),
      }).then((res) => {
        if (res.code === 200) {
          this.$message({message: "编辑成功", type: "success"});
          this.commentItemgetlist(this.PageSize, this.start);
          this.editDialogVisible = false;
        } else {
          this.$message({message: "编辑失败", type: "warning"});
        }
      })
    }
  },
};
</script>
<style lang="scss" scoped>
.mod_box {
  margin-left: 28%;
  width: 44%;
}
.el-inputer {
  width: 80%;
}
</style>
